import { createStore, useStore as baseUseStore } from "vuex";
import type { Store } from "vuex";
import users from "./modules/users";
import signs, { type SignsState } from "./modules/signs";
import type { UserState } from "./modules/users";
import type { InjectionKey } from "vue";
import VuexPersistence from "vuex-persist";
export interface State {}
export interface StateAll extends State {
  users: UserState;
  signs: SignsState;
}
export const key: InjectionKey<Store<StateAll>> = Symbol();
const vuexLocal = new VuexPersistence<State>({
  storage: window.localStorage,
  reducer: (state) => ({ users: { token: (state as StateAll).users.token } }),
});
export function useStore() {
  return baseUseStore(key);
}
export default createStore({
  state: {}, //state 数据储存 (类似存储全局变量的数据)
  getters: {}, //getters 数据获取 (提供用来获取state数据的方法)
  mutations: {}, //mutations 数据改变 (提供存储设置state数据的方法)
  //actions 事件驱动 (提供跟后台接口打交道的方法，并调用mutations提供的方法)
  //-------组件通过dispatch调用actions提供的方法,
  actions: {},

  modules: {
    users,
    signs,
  },
  plugins: [vuexLocal.plugin],
});
